package com.us.usws.common.resourcetree.impl;

import com.us.usws.common.CommonUtils;
import com.us.usws.common.SiteConstants;
import com.us.usws.common.resourcetree.GenericResourceTreeRawDataProvider;
import com.us.usws.common.resourcetree.model.DefaultResourceTree;

/**
 * @author Wang Jianbo
 */
public class ConsoleMenuTreeProvider extends GenericResourceTreeRawDataProvider {

	private long rootId;

	private static final String GET_ALL_SQL = "select id,parent_id,name,display_name,link_url,icon_cls,is_expand FROM sys_obj WHERE type_code='"
			+ SiteConstants.SYSOBJ_TYPE_MENU + "' AND is_visible = 'Y' AND is_enable = 'Y' and -1<>? order by seq_num";

	public ConsoleMenuTreeProvider() {
		this.rootId = SiteConstants.SYSOBJ_CONSOLE_MENU_OBJ_ID;
	}

	public ConsoleMenuTreeProvider(long rootId) {
		this.rootId = rootId;
	}

	@Override
	public String getSQL() {
		//return "SELECT id,parent_id,type_code,display_name,link_url,icon_cls,is_expand FROM sys_obj WHERE type_code='"
		//		+ SiteConstants.SYSOBJ_TYPE_MENU + "' AND is_visible = 'Y' AND is_enable = 'Y' and parent_id=? order by seq_num";
		return GET_ALL_SQL;
	}

	@Override
	public String getKey() {
		return "ConsoleMenu";
	}

	public DefaultResourceTree processRawData(Object[] row) {
		long id = ((Number) row[0]).longValue();
		long parentId = ((Number) row[1]).longValue();
		String name = (String) row[2];
		String text = (String) row[3];
		String iconCls = (String) row[5];
		boolean expand = CommonUtils.toBooleanDefaultFalse((String) row[6]);

		//gen object
		DefaultResourceTree node = new DefaultResourceTree(id, parentId, text);
		node.setName(name);
		node.setIconCls(iconCls);
		if (id == getRootId()) {
			node.setExpanded(true);
		} else {
			node.setExpanded(expand);
		}

		return node;
	}

	@Override
	public long getRootId() {
		return rootId;
	}

	@Override
	public Class<DefaultResourceTree> getNodeClass() {
		return DefaultResourceTree.class;
	}
}
